<?php
    error_reporting(E_ALL ^ E_NOTICE);
    include_once("include/auto_load.php");

    // Autocomplete do Jquery
    if($_REQUEST['term']){
        $q = utf8_decode($_GET['term']);
        $db = new DB();

        // Fornecedor
        if($_REQUEST['FORN']){
            $where = array("(upper(razao) like upper('%{$q}%') or cnpj like '%{$q}%')","ativo is not null");
            $fdao = new FornDAO($db);
            $lista = $fdao->getLista($where);

            if($lista){
                foreach($lista as $l){
                    if(strpos(strtolower($l->getRazao()),strtolower($q)) !== false) {
                        $resul['value'] = utf8_encode($l->getRazao());
                        $resul['id'] = $l->getId();
                        $y[] = $resul;
                    }
                }
            }
        }

        // Marca
        if($_REQUEST['MARCA']){
            $where = array("upper(descr) like upper('%{$q}%')","ativo is not null");
            $mdao = new MarcaDAO($db);
            $lista = $mdao->getLista($where);

            if($lista){
                foreach($lista as $l){
                    if(strpos(strtolower($l->getDescr()),strtolower($q)) !== false) {
                        $resul['value'] = utf8_encode($l->getDescr());
                        $resul['id'] = $l->getId();
                        $y[] = $resul;
                    }
                }
            }
        }

        // Classe
        if($_REQUEST['CLASSE']){
            $where = array("upper(descr) like upper('%{$q}%')");
            $cdao = new ClasseDAO($db);
            $lista = $cdao->getLista($where);

            if($lista){
                foreach($lista as $l){
                    if(strpos(strtolower($l->getDescr()),strtolower($q)) !== false) {
                        $resul['value'] = utf8_encode($l->getDescr());
                        $resul['id'] = $l->getId();
                        $y[] = $resul;
                    }
                }
            }
        }

        // Produto
        if($_REQUEST['MS']){
            $glue = explode("*",$q);
            
            foreach($glue as $g){
                $where[] = "((upper(descr) like upper('%{$g}%')) or (ref like '%{$g}%') or (upper(obs) like upper('%{$g}%')))";                
            }            
            $mdao = new MsDAO($db);
            $lista = $mdao->getLista($where);

            if($lista){
                foreach($lista as $l){                
                    $resul['label'] = utf8_encode($l->getRef()." - ".$l->getDescr());
                    $resul['value'] = utf8_encode($l->getRef()." - ".$l->getDescr() .' - '. $l->getObs());
                    $resul['id'] = $l->getId();
                    $y[] = $resul;                    
                }
            }
        }

        // Cliente
        if($_REQUEST['CLIEN']){            
            $where = array("upper(nome) like upper('%{$q}%')","ativo is not null");
            $cdao = new ClienDAO($db);
            $lista = $cdao->getLista($where);          

            if($lista){
                foreach($lista as $l){
                    if(strpos(strtolower($l->getNome()),strtolower($q)) !== false) {
                        $resul['value'] = utf8_encode($l->getNome());
                        $resul['id'] = $l->getId();
                        $y[] = $resul;
                    }
                }
            }
        }		
        echo json_encode($y);
        exit;
    } 
?>       